package com.beijiteshop.shop.ui.main

import android.os.Bundle
import android.view.KeyEvent
import android.view.View
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import com.beijiteshop.libcommon.base.activity.BaseActivity
import com.beijiteshop.libcommon.utils.EventUtil
import com.beijiteshop.shop.R
import com.beijiteshop.shop.databinding.ActivityMainBinding
import com.beijiteshop.shop.model.event.EventKey
import com.beijiteshop.shop.ui.home.fragment.HomeFragment
import com.beijiteshop.shop.ui.mine.MineFragment
import com.beijiteshop.shop.ui.notification.fragment.NotificationFragment
import com.beijiteshop.shop.ui.shopcar.ShopCarFragment
import com.blankj.utilcode.util.LogUtils
import kotlin.system.exitProcess

/**
 * 首页
 */
class MainActivity : BaseActivity<ActivityMainBinding>(),View.OnClickListener{

    private val mTabList: MutableList<Pair<View,View>> = mutableListOf()
    private val mFragmentList: MutableList<Fragment> = mutableListOf()
    private var mExitTime = 0L

    override fun getLayoutRes(): Int = R.layout.activity_main

    override fun initView(savedInstanceState: Bundle?) {
        initTabView()
    }

    override fun initData() {

    }

    override fun initEvent() {
        mBinding.mainTabHomeLl.setOnClickListener(this)
        mBinding.mainTabNotifyLl.setOnClickListener(this)
        mBinding.mainTabShopCarLl.setOnClickListener(this)
        mBinding.mainTabMineLl.setOnClickListener(this)

        startObserve()
    }

    private fun initTabView(){
        mTabList.add(mBinding.mainTabHomeIv to mBinding.mainTabHomeTv)
        mTabList.add(mBinding.mainTabNotifyIv to mBinding.mainTabNotifyTv)
        mTabList.add(mBinding.mainTabShopCarIv to mBinding.mainTabShopCarTv)
        mTabList.add(mBinding.mainTabMineIv to mBinding.mainTabMineTv)

        initFragments()
        initViewPager()
        //默认选中第一个Tab
        selectTabView(0)
    }

    private fun initFragments(){
        mFragmentList.add(HomeFragment.newInstance())
        mFragmentList.add(NotificationFragment.newInstance())
        mFragmentList.add(ShopCarFragment.newInstance())
        mFragmentList.add(MineFragment.newInstance())
    }

    private fun initViewPager(){
        val pagerAdapter = FragmentPagerAdapter(mFragmentList,supportFragmentManager)
        mBinding.mainVp.adapter = pagerAdapter
        mBinding.mainVp.addOnPageChangeListener(object : ViewPager.OnPageChangeListener{
            override fun onPageScrollStateChanged(state: Int) {

            }

            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {

            }

            override fun onPageSelected(position: Int) {
                selectTabView(position)
            }

        })
    }

    /**
     * 设置Tab的选中效果
     */
    private fun selectTabView(position: Int){
        if (position < mTabList.size){
            mTabList.forEachIndexed { index, _ ->
                mTabList[index].first.isSelected = index == position
                mTabList[index].second.isSelected = index == position
            }
        }
    }

    /**
     * 设置选中的Fragment
     */
    private fun selectViewPager(position: Int){
        mBinding.mainVp.setCurrentItem(position,false)
    }

    override fun onClick(v: View) {
        when(v.id){
            R.id.main_tab_home_ll -> {
                selectTabView(0)
                selectViewPager(0)
            }
            R.id.main_tab_notify_ll -> {
                selectTabView(1)
                selectViewPager(1)
            }
            R.id.main_tab_shop_car_ll -> {
                selectTabView(2)
                selectViewPager(2)
            }
            R.id.main_tab_mine_ll -> {
                selectTabView(3)
                selectViewPager(3)
            }
        }
    }

    private fun startObserve(){

        //选中购物车页面
        EventUtil.observe<Any>(this,EventKey.TO_SHOP_CAR){
            selectTabView(2)
            selectViewPager(2)
        }

        //选中首页
        EventUtil.observe<Any>(this,EventKey.TO_HOME){
            selectTabView(0)
            selectViewPager(0)
        }
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK){
            if (System.currentTimeMillis() - mExitTime > 2000){
                showToast(resources.getString(R.string.exit_app))
                mExitTime = System.currentTimeMillis()
            }else{
                exitProcess(0)
            }
            return true
        }
        return super.onKeyDown(keyCode, event)
    }
}
